package org.esgi.transformer.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.esgi.model.impl.History;
import org.esgi.model.interfaces.IHistory;
import org.esgi.model.interfaces.IQuestion;

import org.esgi.transformer.interfaces.IXMLHistoryTransformer;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLHistoryTransformer extends XMLModelTransformer implements IXMLHistoryTransformer
{
	private XMLQuestionTransformer questionTransformer_;
	
	public static final String XMLHistoryNodeName = "history";
	public static final String XMLHistoryIdAttrName = "id";
	public static final String XMLHistoryFormIdNodeName = "formId";
	public static final String XMLHistoryFormNameNodeName = "formName";
	public static final String XMLHistoryDateStartNodeName = "dateStart";
	public static final String XMLHistoryQuestionsNodeName = "questions";

	public XMLHistoryTransformer(XMLQuestionTransformer questionTransformer)
	{
		questionTransformer_ = questionTransformer;
	}
	
	@Override
	public IHistory historyFromElement(Element e)
	{
		IHistory h = new History();
		h.setId(e.getAttribute(this.addDefaultNamespaceToName(XMLHistoryIdAttrName)));
		NodeList nl = e.getChildNodes();
		for(int i=0 ; i<nl.getLength(); i++)
		{
			Node childNode = nl.item(i);
			if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLHistoryFormIdNodeName)))
			{
				Node textNode = childNode.getFirstChild();
				if(textNode != null)
					h.setFormId(textNode.getNodeValue());
			}
			else if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLHistoryFormNameNodeName)))
			{
				Node textNode = childNode.getFirstChild();
				if(textNode != null)
					h.setFormName(textNode.getNodeValue());
			}
			else if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLHistoryDateStartNodeName)))
			{
				Node textNode = childNode.getFirstChild();
				if(textNode != null)
				{
					String dateStartText = textNode.getNodeValue();
					SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
					try {
						h.setStartingDate(dateFormat.parse(dateStartText));
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
				}
			}
			else if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLHistoryQuestionsNodeName)))
			{
				NodeList nlQuestions = childNode.getChildNodes();
				if(nlQuestions != null)
				{
					ArrayList<IQuestion> questions = new ArrayList<IQuestion>();
					for(int j=0 ; j<nlQuestions.getLength(); j++)
					{
						Node questionNode = nlQuestions.item(j);
						if(questionNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLQuestionTransformer.XMLQuestionNodeName)))
							questions.add(this.questionTransformer_.questionFromElement((Element) questionNode));
					}
					h.setQuestions(questions);
				}
			}
		}
		return h;
	}

	@Override
	public Document historyToDocument(IHistory anHistory, DOMImplementation impl) 
	{
		if((anHistory instanceof IHistory) && impl != null)
		{
			Document doc = impl.createDocument("", this.addDefaultNamespaceToName(XMLHistoryNodeName), null);
			if(doc != null)
			{
				Element historyNode = doc.getDocumentElement();
				historyNode.setAttribute(this.addDefaultNamespaceToName(XMLHistoryIdAttrName), anHistory.getId());
				
				Element formIdNode = doc.createElement(this.addDefaultNamespaceToName(XMLHistoryFormIdNodeName));
				formIdNode.appendChild(doc.createTextNode(anHistory.getFormId()));
				historyNode.appendChild(formIdNode);
				
				Element formNameNode = doc.createElement(this.addDefaultNamespaceToName(XMLHistoryFormNameNodeName));
				formNameNode.appendChild(doc.createTextNode(anHistory.getFormName()));
				historyNode.appendChild(formNameNode);
				
				SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
				Element dateFormatNode = doc.createElement(this.addDefaultNamespaceToName(XMLHistoryDateStartNodeName));
				dateFormatNode.appendChild(doc.createTextNode(dateFormat.format(anHistory.getStartingDate())));
				historyNode.appendChild(dateFormatNode);
				
				Node questionsNode = doc.createElement(this.addDefaultNamespaceToName(XMLHistoryQuestionsNodeName));
				List<IQuestion> questions = anHistory.getQuestions();
				if(questions != null)
				{
					for(int i=0; i<questions.size(); i++)
					{
						Element questionNode = this.questionTransformer_.questionToElement(questions.get(i), doc);
						if(questionNode != null)
							questionsNode.appendChild(questionNode);
					}
				}
				historyNode.appendChild(questionsNode);
				return doc;
			}
		}
		return null;
	}
}
